﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace TCCT_CTC_CSDLDangVienV1.HeThong
{
    public partial class frmLogin : frmBase
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                ApplyUI();
                BindDataToControl();
            }
            catch (Exception _ex)
            {
                ShowErrorMessage(_ex);
            }
        }
        private void ApplyUI()
        {
            UI.TextBox.ApplyDefault(txtUsername);
            UI.TextBox.ApplyPassword(txtPassword);
            UI.CheckBox.ApplyDefault(chNhoMatKhau);
        }

        private void BindDataToControl()
        {
            try
            {
                string sql = "Select * From tblNhoMatKhau ";
                DataTable temDT = OleDBClass.OleReturnDataTable(sql);
                if (temDT.Rows[0]["NhoMatKhau"].ToString() == "True")
                {
                    SetValue(txtUsername, temDT.Rows[0]["MyUsername"].ToString());
                    SetValue(txtPassword, EncryptSMSB.Decrypt(temDT.Rows[0]["MyPassword"].ToString()));
                    SetValue(chNhoMatKhau, Util.ToBool(temDT.Rows[0]["NhoMatKhau"]));

                }
            }
            catch (Exception _ex)
            {
                ShowErrorMessage(_ex);
            }
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            try
            {

                string sql = "Select * From tblUser Where Username=@Username And Password=@Password And IsLocked<>1";
                List<SqlParameter> arrParameters = new List<SqlParameter>();
                arrParameters.Add(SqlHelper.CreateParameterObject("@Username", SqlDbType.NVarChar, ParameterDirection.Input, GetString(txtUsername)));
                arrParameters.Add(SqlHelper.CreateParameterObject("@Password", SqlDbType.NVarChar, ParameterDirection.Input, EncryptSMSB.Encrypt(txtPassword.Text)));
                DataTable temDT = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParameters);
                if (temDT.Rows.Count > 0)
                {
                    MyCommon.CurrentUserID = temDT.Rows[0]["ID"].ToString();
                    MyCommon.CurrentUserHoTen = temDT.Rows[0]["HoTen"].ToString();
                    MyCommon.CurrentUserIsAdministrator = Util.ToBool(temDT.Rows[0]["IsAdministrator"]);
                    MyCommon.CurrentUserIsAdmin= Util.ToBool(temDT.Rows[0]["IsAdmin"]);
                    MyCommon.CurrentUserDauMoiID = temDT.Rows[0]["DauMoiID"].ToString();
                    MyCommon.CurrentPassword = txtPassword.Text;

                    frmMain.Instance.mainLogin.Caption = "Đăng nhập bởi: " + GetString(txtUsername) + " - " + MyCommon.CurrentUserHoTen;

                    sql = "Update tblNhoMatKhau Set MyUsername=@MyUsername, MyPassword=@MyPassword, NhoMatKhau=@NhoMatKhau ";
                    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sql, OleDBClass.OleConn);
                    cmd.Parameters.Add("@MyUsername", System.Data.OleDb.OleDbType.VarWChar).Value = txtUsername.Text.Trim();
                    cmd.Parameters.Add("@MyPassword", System.Data.OleDb.OleDbType.VarWChar).Value = EncryptSMSB.Encrypt(txtPassword.Text);
                    cmd.Parameters.Add("@NhoMatKhau", System.Data.OleDb.OleDbType.Boolean).Value = chNhoMatKhau.Checked;

                    if (OleDBClass.OleConn.State != ConnectionState.Open)
                        OleDBClass.OleConn.Open();
                    cmd.ExecuteNonQuery();

                    sql = "Insert Into tblUserlog(UserID, LoginTime, LogoutTime) Values(@UserID, GetDate(), @LogoutTime) ";
                    arrParameters = new List<SqlParameter>();
                    arrParameters.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.NVarChar, ParameterDirection.Input, MyCommon.CurrentUserID));
                    arrParameters.Add(SqlHelper.CreateParameterObject("@LogoutTime", SqlDbType.DateTime, ParameterDirection.Input, null));
                    SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParameters);


                    MyCommon.ShowMessage("Đăng nhập thành công");
                    this.Close();
                }
                else
                {
                    MyCommon.ShowMessage("Đăng nhập không thành công");
                    txtUsername.Focus();
                }
            }
            catch (Exception _ex)
            {
                ShowErrorMessage(_ex);
            }
        }


        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }



    }
}